package com.wei.czz.framework.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.framework.admin.dao.RoleGroupDao;
import com.wei.czz.framework.admin.entity.RoleGroupEntity;
import com.wei.czz.framework.admin.service.RoleGroupService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-10-22 16:45:50
 * className: RoleSpaceServiceImpl 系统角色授权组与角色关联服务接口实现类
 * version: 1.0
 * description:
 */
@Service("roleGroupService")
@AllArgsConstructor
public class RoleGroupServiceImpl extends ServiceImpl<RoleGroupDao, RoleGroupEntity> implements RoleGroupService {

    private static final Logger log = LoggerFactory.getLogger(RoleGroupServiceImpl.class);

    @Transactional
    @Override
    public void batchSave(List<RoleGroupEntity> roleGroupList) {
        boolean bool = this.saveBatch(roleGroupList);
        log.info("批量保存角色授权组与角色关联完成。bool={}", bool);
    }

    @Override
    public List<RoleGroupEntity> findList(List<Long> spaceIdList, List<Long> roleIdList, Integer deleteStatus) {
        if (CollectionUtils.isEmpty(spaceIdList) && CollectionUtils.isEmpty(roleIdList)) {
            log.info("获取角色授权组关联角色操作，传入角色授权组主键列表和角色主键列表均为空");
            return Collections.emptyList();
        }
        LambdaQueryWrapper<RoleGroupEntity> roleGroupLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleGroupLambdaQueryWrapper.in(!CollectionUtils.isEmpty(spaceIdList), RoleGroupEntity::getSpaceId, spaceIdList)
                .in(!CollectionUtils.isEmpty(roleIdList), RoleGroupEntity::getRoleId, roleIdList)
                .eq(Objects.nonNull(deleteStatus), RoleGroupEntity::getDeleteStatus, deleteStatus);
        // 查询
        List<RoleGroupEntity> roleGroupList = baseMapper.selectList(roleGroupLambdaQueryWrapper);
        log.info("查询角色授权组与角色关联完成。spaceIdList={} roleIdList={} deleteStatus={} list.size={}", spaceIdList,
                roleIdList, deleteStatus, roleGroupList.size());
        return roleGroupList;
    }

    @Transactional
    @Override
    public void batchUpdate(List<RoleGroupEntity> roleGroupList) {
        boolean bool = this.updateBatchById(roleGroupList);
        log.info("批量修改角色授权组与角色关联完成。bool={}", bool);
    }

    @Override
    public void delete(List<Long> spaceIdList, List<Long> roleIdList) {
        if (CollectionUtils.isEmpty(spaceIdList) && CollectionUtils.isEmpty(roleIdList)) {
            log.info("删除角色授权组与角色关联数据，传入参数为空");
            return;
        }
        // 获取操作用户
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        LambdaUpdateWrapper<RoleGroupEntity> roleGroupLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        roleGroupLambdaUpdateWrapper.set(RoleGroupEntity::getDeleteStatus, CommonEnum.ONE.getValue())
                .set(RoleGroupEntity::getUpdateTime, date)
                .set(RoleGroupEntity::getUpdateUser, userPo.getUsername())
                .set(RoleGroupEntity::getUpdateUserId, userPo.getUserId())
                .in(!CollectionUtils.isEmpty(spaceIdList), RoleGroupEntity::getSpaceId, spaceIdList)
                .in(!CollectionUtils.isEmpty(roleIdList), RoleGroupEntity::getRoleId, roleIdList)
                .eq(RoleGroupEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 删除
        int count = baseMapper.update(roleGroupLambdaUpdateWrapper);
        log.info("删除角色授权组与角色关联数据完成。spaceIdList={} roleIdList={} count={}", spaceIdList, roleIdList, count);
    }
}